import { PrismaClient, Prisma } from '@prisma/client'
import { hash } from 'bcryptjs'
import { UserRole } from '@/types'

const prisma = new PrismaClient()

async function main() {
  // 创建管理员用户
  const hashedPassword = await hash('admin123', 10)
  const admin = await prisma.user.create({
    data: {
      username: 'admin',
      password: hashedPassword,
      name: '管理员',
      role: 'ADMIN',
      status: true
    }
  })

  // 创建默认商品分类
  const categories = [
    { name: '食品饮料' },
    { name: '生活用品' },
    { name: '服装鞋帽' },
    { name: '数码电器' },
    { name: '美妆护理' },
    { name: '母婴用品' },
    { name: '图书文具' },
    { name: '运动户外' },
  ]

  console.log('Creating categories...') // 调试用
  for (const category of categories) {
    await prisma.category.upsert({
      where: { name: category.name },
      update: {},
      create: category,
    })
  }
  console.log('Categories created') // 调试用

  // 获取默认分类
  const defaultCategory = await prisma.category.findFirst({
    where: { name: '生活用品' }
  })

  // 创建示例商品
  if (defaultCategory) {
    await prisma.product.create({
      data: {
        name: '示例商品',
        code: 'DEMO001',
        price: new Prisma.Decimal(99.99),
        stock: 100,
        unit: '件',
        categoryId: defaultCategory.id,
      }
    })
  }

  console.log('数据库初始化完成')
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  }) 